package org.apache.james.mime4j.message;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.james.mime4j.decoder.CodecUtil;
import org.apache.james.mime4j.message.storage.TempFile;
import org.apache.james.mime4j.message.storage.TempPath;
import org.apache.james.mime4j.message.storage.TempStorage;

class TempFileBinaryBody extends AbstractBody
  implements BinaryBody
{
  private TempFile tempFile = null;

  public TempFileBinaryBody(InputStream paramInputStream)
    throws IOException
  {
    this.tempFile = TempStorage.getInstance().getRootTempPath().createTempFile("attachment", ".bin");
    OutputStream localOutputStream = this.tempFile.getOutputStream();
    CodecUtil.copy(paramInputStream, localOutputStream);
    localOutputStream.close();
  }

  public void dispose()
  {
    if (this.tempFile != null)
    {
      this.tempFile.delete();
      this.tempFile = null;
    }
  }

  public InputStream getInputStream()
    throws IOException
  {
    return this.tempFile.getInputStream();
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException
  {
    CodecUtil.copy(getInputStream(), paramOutputStream);
  }
}